package com.mk.settlement.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mk.settlement.common.Util;
import com.mk.settlement.enums.SettlementStateEnums;
import com.mk.settlement.enums.SettlementTypeEnums;
import com.mk.settlement.mappers.SettlementOrderMapper;
import com.mk.settlement.mappers.SettlementTempResultMapper;
import com.mk.settlement.model.SettlementTempResult;
import com.mk.settlement.service.SettlementRegularService;

public class SettlementThresholdService implements SettlementRegularService {

    private Logger log = LoggerFactory.getLogger(SettlementThresholdService.class);

    private SettlementTempResultMapper settlementTempResultMapper;

    private SettlementOrderMapper settlementOrderMapper;
    
    /*
     * 可选值:SettlementTypeEnums.THRESHOLD_MONTH/SettlementTypeEnums.THRESHOLD_WEEK
     * 
     * 根据传入值不同,此service可阈值月结/阈值周结共用
     */
    private SettlementTypeEnums thresholdType;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");

    @Override
    public void doSettlement() {

        String desc = thresholdType.getDesc();
        log.info(desc + "worker开始执行...");

        log.info("从基础表中获取未统计" + desc + "数据...");
        List<SettlementTempResult> results = settlementTempResultMapper.getThresholdTempFromOrder(thresholdType.getIndex(),
                SettlementStateEnums.NotStatistics.getIndex(), sdf.format(new Date()));

        log.info("从基础表中获取未统计" + desc + "数据[" + results.size() + "]条.");

        log.info("向阈值临时结果表中插入获取到的数据...");
        
        for (SettlementTempResult r : results) {

            try {
                
                Date endDate = settlementTempResultMapper.getAlreadyStatisticsDate(r.getSettlementTargetId(), r.getSettlementCategory(), r.getType(),
                        r.getSaleType(), SettlementStateEnums.AlreadyStatistics.getIndex(), r.getSettlementType(), Util.getDay(thresholdType));
                
                if( null != endDate) {
                    r.setBeginTime(endDate);
                }
                
                settlementTempResultMapper.insertOrUpdateSum(r);

            } catch (Exception e) {
                log.error("向阈值临时结果表中插入数据异常!", e);
                
                throw e;
            }
        }

        log.info("向阈值临时结果表中插入获取到的数据完毕.");

        log.info("更新已经统计过的基础数据状态...");
        try {
            settlementOrderMapper.updateSettlementState(SettlementStateEnums.NotStatistics.getIndex(),
                    SettlementStateEnums.AlreadyStatistics.getIndex(), thresholdType.getIndex(), sdf.format(new Date()), null);
        } catch (Exception e) {
            log.error("更新已经统计过的基础数据状态异常!", e);
            throw e;
        }
        log.info("更新已经统计过的基础数据状态完毕.");

        log.info(desc + "worker执行完毕.");
    }

    public void setSettlementTempResultMapper(SettlementTempResultMapper settlementTempResultMapper) {
        this.settlementTempResultMapper = settlementTempResultMapper;
    }

    public void setSettlementOrderMapper(SettlementOrderMapper settlementOrderMapper) {
        this.settlementOrderMapper = settlementOrderMapper;
    }

    public void setThresholdType(SettlementTypeEnums thresholdType) {
        this.thresholdType = thresholdType;
    }
}
